﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

namespace WpfStarterKit.Framework.Navigation
{
    /// <summary>
    /// Custom attribute that can be used to decorate methods that require a wait overlay to be shown
    /// </summary>
    [AttributeUsage(AttributeTargets.Method)]
    public sealed class LongRunningOperationAttribute : Attribute
    {
        /// <summary>
        /// An optional loading message that needs to be shown
        /// </summary>
        public string Message { get; set; }

        /// <summary>
        /// This helper method can be used to get a WaitOverlayAttribute instance that was used to decorate a method
        /// </summary>
        /// <param name="memberInfo">Method information. Can be Type or MethodInfo</param>
        /// <returns>An instance WaitOverlayAttribute if it was found on the method, null otherwise</returns>
        public static LongRunningOperationAttribute GetAttribute(MemberInfo memberInfo)
        {
            object[] waitAttributes = memberInfo.GetCustomAttributes(typeof(LongRunningOperationAttribute), false);
            if (waitAttributes != null && waitAttributes.Count() == 1)
            {
                return waitAttributes[0] as LongRunningOperationAttribute;
            }
            else
            {
                return null;
            }
        }
    }
}
